#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>

#include "config.h"
#include "sensor_data.h"
#include "oled.h"
#include "MQTT.h"

TaskHandle_t taskHandle; // 计算task的空间大小

void connectwifi(); // 连接WIFI

void setup()
{
  Serial.begin(115200);
  // Wire.begin(SDA,SCL);
  Wire.begin();
  // connectwifi();

  xMutexSensor = xSemaphoreCreateMutex(); // 创建MUTEX

  xTaskCreatePinnedToCore(MQTT, "MQTT Server", 1024 * 6, NULL, 1, NULL, 1);

  xTaskCreatePinnedToCore(oledTask, "OLED display", 1024 * 6, (void *)&data, 1, &taskHandle, 1);

  xTaskCreatePinnedToCore(PCB_temp, "10K NTC", 1024 * 2, NULL, 1, NULL, 1);

  xTaskCreatePinnedToCore(MPPT, "MPPT control", 1024 * 6, NULL, 1, NULL, 1);

  int waterMark = uxTaskGetStackHighWaterMark(taskHandle);
  Serial.print(F("Task Free Memory: ")); // 任务剩余空间
  Serial.print(waterMark);
  Serial.println(F(" Bytes"));
  vTaskDelete(NULL); // 删除setup()这个任务
}

void loop() {}

void connectwifi()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    vTaskDelay(500);
    Serial.print(F("."));
  }
  Serial.println(F(""));
  Serial.println(F("WiFi Connected!"));
  Serial.println(F(""));
}